package com.oocl.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.dao.GoodsDao;
import com.oocl.dao.WareHouseDao;
import com.oocl.dao.impl.GoodsDaoImpl;
import com.oocl.dao.impl.WareHouseDaoImpl;
import com.oocl.pojo.Goods;

public class Update2Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private GoodsDao goodsDao;
	private WareHouseDao wareHouseDao;

    public Update2Servlet() {
        super();
        goodsDao = new GoodsDaoImpl();
        wareHouseDao = new WareHouseDaoImpl();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Goods temp = (Goods)request.getSession().getAttribute("goods");
		String wareHouse = request.getParameter("wareHouse");
		String goodName = request.getParameter("name");
		if(request.getParameter("price") == null)
			return;
		double price = Double.parseDouble(request.getParameter("price"));
		int count = Integer.parseInt(request.getParameter("count"));
		String cpd = request.getParameter("pd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date pd = null;
		try {
			pd = sdf.parse(cpd);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Goods good = new Goods(temp.getId(), goodName, price, count, pd, wareHouseDao.findIdByAddr(wareHouse));
		request.getSession().setAttribute("uid", wareHouseDao.findIdByAddr(wareHouse));
		goodsDao.updateGoods(good);
		List<Goods> list = goodsDao.getGoodsFromWareHouse(wareHouseDao.findIdByAddr(wareHouse));
		request.getSession().setAttribute("list", list);
		request.getRequestDispatcher("view").forward(request, response);
	}

}
